package whu.cn.whushare.ui.main.my.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import com.androidplus.util.ViewMapping;
import com.androidplus.util.ViewMappingUtil;

import java.io.InputStream;
import java.net.URL;
import java.util.List;

import whu.cn.whushare.R;
import whu.cn.whushare.bean.User;
import whu.cn.whushare.common.ImageWorkerManager;
import whu.cn.whushare.ui.main.my.bean.MyAnswer;
import whu.cn.whushare.ui.main.my.bean.MyDynamic;
import whu.cn.whushare.util.Utils;

/**
 * Created by xiaoping on 2015/4/11.
 */
public class MyDynamicListAdapter extends BaseAdapter {
    private Context mContext;
    private List<MyDynamic> mMyDynamic;

    public MyDynamicListAdapter(Context mContext, List<MyDynamic> mMyDynamic) {
        this.mContext = mContext;
        this.mMyDynamic = mMyDynamic;
    }

    @Override
    public int getCount() {
        return mMyDynamic.size();
    }

    @Override
    public Object getItem(int position) {
        return mMyDynamic.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.layout_my_dynamic_item, null);
            viewHolder = new ViewHolder();
            ViewMappingUtil.mapView(viewHolder, convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        MyDynamic myDynamic = mMyDynamic.get(position);
       viewHolder.agree_count.setText( 0+ "");
        switch (myDynamic.userDynamicData.id.type)
        {
            case "focus_question_answer":
                viewHolder.type.setText( "用户关注的问题有了新的回答");
                break;
            case "focus_user_question":
                viewHolder.type.setText( "用户关注的用户有了新的问题");
                break;
            case "focus_user_answer":
                viewHolder.type.setText( "用户关注的用户有了新的回答");
                break;
            case "focus_topic_question":
                viewHolder.type.setText( "用户关注的话题有了新的回答");
                break;
            default:
                break;

        }


        viewHolder.title.setText(myDynamic.sourceQuestion.getQuestionName()+"");
        final String html=myDynamic.userDynamicData.dataDesc+"";



        new AsyncTask<String,String,Spanned>(){
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }

            @Override
            protected void onPostExecute(Spanned s) {
                super.onPostExecute(s);
               viewHolder.content.setText(s);


            }

            @Override
            protected Spanned doInBackground(String... params) {
                Spanned sp = Html.fromHtml(html,
                        new Html.ImageGetter() {
                            @Override
                            public Drawable getDrawable(String source) {
                                source = "http://www.027team.com" + source;
                                InputStream is = null;
                                try {
                                    is = new URL(source).openStream();
                                    Drawable d = Drawable.createFromStream(is, "src");
                                    d.setBounds(0, 0, d.getIntrinsicWidth() * 3, d.getIntrinsicHeight() * 3);
                                    is.close();
                                    return d;
                                } catch (Exception e) {
                                    return null;
                                }
                            }
                        }, null);
                return sp;
            }
        }.execute();


        viewHolder.care_count.setText(myDynamic.attentionNum + "个关注");
        viewHolder.answer_count.setText(myDynamic.answerNum+"个回答");
        User user = myDynamic.dataUserInfo;
        if (!TextUtils.isEmpty(user.getAvatar())) {
            ImageWorkerManager.getInstance(mContext).loadCommonImage(Utils.getPhotoUrl(user.getAvatar()),
                    viewHolder.other_Avatar, R.drawable.avatar, R.drawable.avatar);
        }
        return convertView;
    }

   public class ViewHolder {
        @ViewMapping(id = R.id.other_Avatar)
        private ImageView other_Avatar;
        @ViewMapping(id = R.id.type)
        private TextView type;
        @ViewMapping(id = R.id.title)
        private TextView title;
        @ViewMapping(id = R.id.answer_count)
        private TextView answer_count;
        @ViewMapping(id=R.id.care_count)
        private TextView care_count;
        @ViewMapping(id=R.id.agree_count)
        private TextView agree_count;
        @ViewMapping(id=R.id.content)
        private TextView content;


    }

}


